package cn.newtouch.system.controller;

import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.system.domain.SystemDictionaryDetail;
import cn.newtouch.system.query.SystemDictionaryDetailQuery;
import cn.newtouch.system.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(value = "数据字典类型的API",description="类型相关的CRUD功能")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionarydetailService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有类型" )
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionarydetailService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryDetail findOne(@PathVariable Long id){
       return systemDictionarydetailService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            systemDictionarydetailService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    public JsonResult updateOrAdd(@RequestBody SystemDictionaryDetail systemDictionarydetail){
        try {
            if(systemDictionarydetail.getId()==null){
                //添加操作
                systemDictionarydetailService.save(systemDictionarydetail);
            }else{
                //修改操作
                systemDictionarydetailService.update(systemDictionarydetail);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery query){
        try {
            return systemDictionarydetailService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            systemDictionarydetailService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }


}
